package com.dkm.orproject.dkmoa.service.serviceimpl;

import com.dkm.orproject.dkmoa.dao.OaCarTypeMapper;
import com.dkm.orproject.dkmoa.domain.OaCarType;
import com.dkm.orproject.dkmoa.domain.OaPersonnel;
import com.dkm.orproject.dkmoa.domain.OaVoteType;
import com.dkm.orproject.dkmoa.service.OaCarTypeService;
import com.dkm.orproject.utils.utils.IdToobtainUtils;
import com.dkm.orproject.utils.utils.ThreeVo;
import com.dkm.orproject.utils.utils.Tools;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName : OaCarTypeServiceImpl  //类名
 * @Description :   //描述
 * @Author : 易嵘  //作者
 * @Date: 2021-01-20 11:32  //时间
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class OaCarTypeServiceImpl implements OaCarTypeService {


    @Resource
    OaCarTypeMapper carTypeMapper;

    @Resource
    IdToobtainUtils idToobtainUtils;

    @Resource
    Tools tools;

    @Override
    public List<OaCarType> findAll(String carTypeName, String startTime, String endTime, Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        return carTypeMapper.findAll(carTypeName,startTime,endTime);
    }

    @Override
    public Long rowCount(String carTypeName, String startTime, String endTime) {
        return carTypeMapper.rowCount(carTypeName,startTime,endTime);
    }

    /**
     * 查询所有
     * @param id 车辆类型id
     * @param show 查看判断关键字
     * @return 投票类型树
     */
    @Override
    public List<ThreeVo> loadCarType(Long id, String show) {
        OaCarType voteType = carTypeMapper.selectByPrimaryKey(id);
        //查询所有
        List<OaCarType> voteTypes;
        if(StringUtil.isEmpty(show)){
            voteTypes = carTypeMapper.loadCarType(id);
        }else{
            voteTypes = carTypeMapper.loadCarType(null);
        }

        //树状图模型
        List<ThreeVo> threeVos = new ArrayList<>();
        for (OaCarType oaVoteType : voteTypes) {
            ThreeVo threeVo = new ThreeVo();
            if("1".equals(show)){
                if(voteType!=null && voteType.getCarTypeSuperior().equals(oaVoteType.getPkCarTypeId())){
                    //设置默认值
                    threeVo.setSelected(true);
                }
            }else if("2".equals(show)){
                if(voteType!=null && voteType.getPkCarTypeId().equals(oaVoteType.getPkCarTypeId())){
                    //设置默认值
                    threeVo.setSelected(true);
                }
            }
            //参数
            threeVo.setValue(oaVoteType.getPkCarTypeId());
            //显示内容
            threeVo.setName(oaVoteType.getCarTypeName());
            //上级id
            threeVo.setSuperId(oaVoteType.getCarTypeSuperior());
            //加入树状图
            threeVos.add(threeVo);
        }
        return tools.createTree(threeVos);
    }

    @Override
    public OaCarType selectByPrimaryKey(Long id) {
        return carTypeMapper.selectByPrimaryKey(id);
    }

    /**
     * 新增车辆类型
     * @param car 车辆类型
     * @param personnel 创建人
     * @return 回调函数
     */
    @Override
    public int add(OaCarType car, OaPersonnel personnel) {
        car.setPkCarTypeId(idToobtainUtils.getNumberId());
        car.setCreatedBy(personnel.getPersonnelName());
        if(car.getCarTypeSuperior()==null){
            car.setCarTypeSuperior(0L);
        }
        car.setIsRevision("1");
        return carTypeMapper.insertSelective(car);
    }

    @Override
    public int updateByPrimaryKeySelective(OaCarType car) {
        return carTypeMapper.updateByPrimaryKeySelective(car);
    }

    @Override
    public List<OaCarType> selectAll(List<Long> list) {
        return carTypeMapper.selectAll(list);
    }

    @Override
    public int deleteAll(List<Long> list) {
        return carTypeMapper.deleteAll(list);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {
        return carTypeMapper.deleteByPrimaryKey(id);
    }
}
